package com.yc.videosqllite.model;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.Serializable;
import java.util.Objects;

/**
 * 必须
 *
 * @since 2021-05-10
 */
public class VideoLocation implements Serializable, Cloneable {
    /**
     * 视频链接
     */
    private String url;
    /**
     * 视频链接md5
     */
    private String urlMd5;
    /**
     * 视频播放位置
     */
    private long position;
    /**
     * 视频总时间
     */
    private long totalTime;

    /**
     * VideoLocation
     */
    public VideoLocation() {
    }

    /**
     * VideoLocation
     *
     * @param url url
     * @param position position
     * @param totalTime totalTime
     */
    public VideoLocation(String url, long position, long totalTime) {
        this.url = url;
        this.position = position;
        this.totalTime = totalTime;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlMd5() {
        return urlMd5;
    }

    public void setUrlMd5(String urlMd5) {
        this.urlMd5 = urlMd5;
    }

    public long getPosition() {
        return position;
    }

    public void setPosition(long position) {
        this.position = position;
    }

    public long getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
    }

    /**
     * toJson
     *
     * @return return
     */
    public String toJson() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("url", getUrl());
        jsonObject.addProperty("urlMd5", getUrlMd5());
        jsonObject.addProperty("position", getPosition());
        jsonObject.addProperty("totalTime", getTotalTime());
        return jsonObject.toString();
    }

    /**
     * toObject
     *
     * @param jsonStr jsonStr
     * @return return
     */
    public static VideoLocation toObject(String jsonStr) {
        Gson gson = new Gson();
        VideoLocation location = gson.fromJson(jsonStr, VideoLocation.class);
        return location;
    }

    @Override
    public String toString() {
        return "VideoLocation{"
            +
            "url='" + url + '\''
            +
            ", urlMd5='" + urlMd5
            + '\''
            +
            ", position=" + position
            +
            ", totalTime=" + totalTime
            +
            '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        VideoLocation location = (VideoLocation) o;
        return position == location.position
            &&
            totalTime == location.totalTime
            &&
            Objects.equals(url, location.url)
            &&
            Objects.equals(urlMd5, location.urlMd5);
    }

    @Override
    public int hashCode() {
        return Objects.hash(url, urlMd5, position, totalTime);
    }

    /**
     * 重写clone()方法
     * 浅拷贝
     *
     * @return return
     */
    public Object clone() {
        // 浅拷贝
        try {
            // 直接调用父类的clone()方法
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    /**
     * 重写clone()方法
     * 深拷贝
     *
     * @return return
     */
    public Object deepClone() {
        // 深拷贝，创建拷贝类的一个新对象，这样就和原始对象相互独立
        VideoLocation location = new VideoLocation(url, position, totalTime);
        return location;
    }

    /**
     * 比较两个对象
     *
     * @param a a对象
     * @param b b对象
     * @return return
     */
    private boolean equals(Object a, Object b) {
        boolean ab = (a == b);
        boolean equal = (a != null && a.equals(b));
        return ab || equal;
    }

    /**
     * hash算法
     *
     * @param values 参数
     * @return return
     */
    private int hash(Object... values) {
        return hashCode(values);
    }

    /**
     * hashCode
     *
     * @param a a
     * @return return
     */
    public static int hashCode(Object a[]) {
        if (a == null) {
            return 0;
        }
        int result = 1;
        for (Object element : a) {
            result = 31 * result + (element == null ? 0 : element.hashCode());
        }
        return result;
    }
}
